import 'package:flutter/material.dart';
import 'package:flutterapp/src/utils/toast_utils.dart';

main() {
  runApp(
    MaterialApp(
      debugShowCheckedModeBanner: false,
      home: HomePage(),
    ),
  );
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  List<LevelModel> list = [];
  @override
  void initState() {
    super.initState();
    for (int i = 0; i < 20; i++) {
      List<String> level = i % 2 == 0 ? ["二级页面1", "二级页面2"] : [];
      list.add(new LevelModel("level-$i", level));
    }
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("二级分类"),
      ),
      body: buildBody(),
    );
  }

  buildBody() {
    return ListView.builder(
        itemCount: list.length,
        itemBuilder: (BuildContext context, int index) {
          LevelModel item = list[index];
          return BuildLevel1Widget(item);
        });
  }
}

class LevelModel {
  //工作名称
  String title;
  //工作部门
  List<String> list;
  LevelModel(this.title, this.list);
}

class BuildLevel1Widget extends StatefulWidget {
  final LevelModel level;
  BuildLevel1Widget(this.level);

  @override
  _BuildLevel1WidgetState createState() => _BuildLevel1WidgetState();
}

class _BuildLevel1WidgetState extends State<BuildLevel1Widget>
    with SingleTickerProviderStateMixin {
  AnimationController _animationController;
  bool isExpanded = false;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _animationController = new AnimationController(
        lowerBound: 0.0,
        upperBound: 0.25,
        duration: Duration(milliseconds: 1000),
        vsync: this);
  }

  @override
  void dispose() {
    super.dispose();
    _animationController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return buildLevel1Widget(widget.level);
  }

  buildLevel1Widget(LevelModel level) {
    return InkWell(
      onTap: () {
        if (_animationController.isCompleted) {
          isExpanded = false;
          _animationController.reverse();
        } else {
          isExpanded = true;
          _animationController.reset();
          _animationController.forward();
        }
        setState(() {});
      },
      child: Container(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            buildLevel1Item(level),
            buildLevel2Wiget(level),
            SizedBox(height: 10),
            Divider()
          ],
        ),
      ),
    );
  }

  buildLevel2Wiget(LevelModel level) {
    if (!isExpanded) return Container();
    if (level.list.length == 0) {
      ToastUtils.showToast("暂无二级数据");
      return Container();
    }
    return ListView.builder(
        itemCount: level.list.length,
        itemBuilder: (BuildContext context, int index) {
          return Container(child: Text(level.list[index]));
        });
  }

  buildLevel1Item(LevelModel level) {
    return Row(
      mainAxisSize: MainAxisSize.min,
      children: [
        Expanded(
          child: Text(level.title, style: TextStyle(fontSize: 20)),
        ),
        RotationTransition(
          turns: _animationController,
          child: Icon(Icons.arrow_right),
        ),
      ],
    );
  }
}
